package com.zzyl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Map;

public class WeatherApiExample {
    @Test
    public void testWeather() {
        String host = "https://weather110.market.alicloudapi.com";
        String path = "/getWeather_40day";
        String appcode = "6b9b69b2a62d4a7c9679f86bcaf97b1a"; // 请替换为实际的AppCode

        // 创建请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "APPCODE " + appcode);

        // 创建查询参数
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("cityID", "101180101");
        queryParams.put("cityName", "郑州");

        try {
            // 拼接完整URL
            String url = host + path;

            // 使用HttpRequest对象构建请求，兼容所有Hutool版本
            HttpRequest request = HttpRequest.get(url);
            // 添加请求头
            request.addHeaders(headers);
            // 添加查询参数
            request.form(queryParams);
            // 设置超时时间
            request.timeout(30000);

            // 执行请求并获取响应
            HttpResponse response = request.execute();
            String result = response.body();

            // 打印响应结果
            System.out.println("响应结果:");
            System.out.println(result);

        } catch (Exception e) {
            System.err.println("请求发生异常:");
            e.printStackTrace();
        }
    }
}
